package com.zqh.semaphore;

import java.util.concurrent.Semaphore;

/**
 * @author 曾庆红
 * {@code @date} 2023-10-14 17:13
 * Semaphore 的基本使用方法
 */

public class TestSemaphore {


    public static void main(String[] args) {
        Semaphore s = new Semaphore(2);
        new Thread(()->{
            try {
                s.acquire();
                System.out.println("t1 running...");
                Thread.sleep(200);
                System.out.println("T1 running");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                s.release();
            }
        }).start();

        new Thread(()->{
            try {
                s.acquire();
                System.out.println("t2 running...");
                Thread.sleep(200);
                System.out.println("t2 running");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                s.release();
            }
        }).start();
    }
}
